import 'dart:ffi';

import 'package:bilibili/http/core/hi_net.dart';
import 'package:bilibili/http/dao/login_dao.dart';
import 'package:bilibili/http/request/registration_request.dart';
import 'package:bilibili/navigator/hi_navigator.dart';
import 'package:bilibili/page/login_page.dart';
import 'package:bilibili/util/color.dart';
import 'package:bilibili/util/string_urtil.dart';
import 'package:bilibili/widget/appbar.dart';
import 'package:bilibili/widget/login_effect.dart';
import 'package:bilibili/widget/login_input.dart';
import 'package:flutter/material.dart';

class RegistrationPage extends StatefulWidget {
  const RegistrationPage({super.key});

  @override
  State<StatefulWidget> createState() {
    return _RegistrationPageState();
  }
}

class _RegistrationPageState extends State<RegistrationPage> {
  bool isProtect = false;
  bool loginEnable = false;

  String imoocId = "";
  String username = "";
  String password = "";
  String rePassword = "";

  @override
  Widget build(BuildContext content) {
    return Scaffold(
      appBar: appBar("注册", "登录", () {
        Navigator.of(context)
            .push(MaterialPageRoute(builder: (context) => LoginPage()));
      }),
      body: ListView(
        children: [
          LoginEffect(protect: isProtect),
          LoginInput("用户名", "请输入用户名", linStretch: true, onChanged: (text) {
            username = text;
            checkInput();
          }),
          LoginInput(
            '密码',
            '请输入密码',
            linStretch: true,
            obscureText: true,
            foucusChanged: (value) => {
              setState(() {
                isProtect = value;
              })
            },
            onChanged: (text) {
              password = text;
              checkInput();
            },
          ),
          LoginInput(
            '密码',
            '请输入密码',
            linStretch: true,
            obscureText: true,
            foucusChanged: (value) => {
              setState(() {
                isProtect = value;
              })
            },
            onChanged: (text) {
              rePassword = text;
              checkInput();
            },
          ),
          LoginInput("慕课网id", "请输入慕课网id",
              linStretch: true, onChanged: (text) => {username = text}),
          Padding(
            padding: EdgeInsets.all(20),
            child: _loginButton(),
          )
        ],
      ),
    );
  }

  void checkInput() {
    bool enable = false;
    if (isNotEmpty(username) &&
        isNotEmpty(password) &&
        isNotEmpty(rePassword)) {
      enable = true;
    }

    setState(() {
      loginEnable = enable;
    });
  }

  _loginButton() {
    return InkWell(
        onTap: () {
          if (loginEnable) {
            checkParams();
          }
        },
        child: Container(
          decoration: BoxDecoration(
              color: loginEnable ? primary : primary[50],
              borderRadius: BorderRadius.circular(4)),
          alignment: Alignment.center,
          height: 42,
          child: Text(
            "注册",
            style: TextStyle(fontSize: 16, color: white),
          ),
        ));
  }

  void checkParams() {
    String tips = '';
    if (password != rePassword) {
      tips = '密码不一致';
    }

    if (tips != '') {
      return;
    }

    send();
  }

  void send() async {
    try {
      var ret = LoginDao.registration(username, password);
      if (ret != null) {
        HiNavigator.getInstance().onJumpTo(RouteStatus.login);
      }
    } catch (e) {
      print(e);
    }
  }
}
